package com.xinqi.modules.stats.score.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xinqi.common.mybatis.data.service.impl.BaseServiceImpl;
import com.xinqi.common.mybatis.utils.Queries;
import com.xinqi.modules.course.clazz.client.ClassClient;
import com.xinqi.modules.course.clazz.dto.rsp.ClassResultDTO;
import com.xinqi.modules.stats.score.convert.CourseTotalScoreTypeConfigConvert;
import com.xinqi.modules.stats.score.domain.CourseTotalScoreBaseConfigEntity;
import com.xinqi.modules.stats.score.domain.CourseTotalScoreTypeConfigEntity;
import com.xinqi.modules.stats.score.dto.req.CourseTotalScoreConfigClassDTO;
import com.xinqi.modules.stats.score.dto.req.CourseTotalScoreConfigInitDTO;
import com.xinqi.modules.stats.score.dto.req.CourseTotalScoreConfigSetDTO;
import com.xinqi.modules.stats.score.dto.rsp.CourseTotalScoreConfigResultDTO;
import com.xinqi.modules.stats.score.mapper.CourseTotalScoreBaseConfigMapper;
import com.xinqi.modules.stats.score.mapper.CourseTotalScoreTypeConfigMapper;
import com.xinqi.modules.stats.score.service.CourseTotalScoreTypeConfigService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 总成绩各分类栏目等级制分值配置表服务层实现
 *
 * @author: dzy
 * @date: 2023/03/26
 */
@Service("courseTotalScoreTypeConfigService")
@RequiredArgsConstructor
public class CourseTotalScoreTypeConfigServiceImpl extends BaseServiceImpl<CourseTotalScoreTypeConfigMapper, CourseTotalScoreTypeConfigEntity> implements CourseTotalScoreTypeConfigService {

    private final CourseTotalScoreTypeConfigConvert courseTotalScoreTypeConfigConvert;

    private final ClassClient classClient;

    private final CourseTotalScoreBaseConfigMapper courseTotalScoreBaseConfigMapper;

    /**
     * 初始化课程栏目等级分制配置
     */
    @Override
    public List<CourseTotalScoreConfigResultDTO> initCourseTotalScoreTypeConfig(CourseTotalScoreConfigInitDTO dto) {
        ClassResultDTO classResultDTO = classClient.findClass(dto.getClassId()).getData();
        if (classResultDTO != null) {
            dto.setCourseId(classResultDTO.getCourseId());
        }
        // 先删除
        CourseTotalScoreConfigClassDTO classDTO = new CourseTotalScoreConfigClassDTO();
        classDTO.setClassId(dto.getClassId());
        classDTO.setType(dto.getType());
        this.baseMapper.deleteCourseTypeConfig(classDTO);
        // 后初始化
        LambdaQueryWrapper<CourseTotalScoreBaseConfigEntity> wrapper = Queries.lambda();
        wrapper.orderByAsc(CourseTotalScoreBaseConfigEntity::getSort);
        List<CourseTotalScoreBaseConfigEntity> baseConfigEntities = courseTotalScoreBaseConfigMapper.selectList(wrapper);
        List<CourseTotalScoreTypeConfigEntity> typeConfigEntities = baseConfigEntities.stream().map(v -> {
            CourseTotalScoreTypeConfigEntity typeConfigEntity = courseTotalScoreTypeConfigConvert.convert(v);
            typeConfigEntity.setId(null);
            typeConfigEntity.setCourseId(dto.getCourseId());
            typeConfigEntity.setClassId(dto.getClassId());
            typeConfigEntity.setType(dto.getType());
            return typeConfigEntity;
        }).collect(Collectors.toList());
        if (this.saveBatch(typeConfigEntities)) {
            return courseTotalScoreTypeConfigConvert.convert(typeConfigEntities);
        } else {
            return null;
        }
    }

    /**
     * 查询课程栏目等级分制
     */
    @Override
    public List<CourseTotalScoreConfigResultDTO> courseTotalScoreTypeConfig(CourseTotalScoreConfigClassDTO dto) {
        LambdaQueryWrapper<CourseTotalScoreTypeConfigEntity> wrapper = Queries.lambda();
        wrapper.eq(CourseTotalScoreTypeConfigEntity::getClassId, dto.getClassId());
        wrapper.eq(CourseTotalScoreTypeConfigEntity::getType, dto.getType());
        wrapper.orderByAsc(CourseTotalScoreTypeConfigEntity::getSort);
        List<CourseTotalScoreConfigResultDTO> list = this.baseMapper.selectDtoList(wrapper, courseTotalScoreTypeConfigConvert::convert);
        if (list == null || list.size() <= 0) {
            ClassResultDTO classResultDTO = classClient.findClass(dto.getClassId()).getData();
            CourseTotalScoreConfigInitDTO initDTO = new CourseTotalScoreConfigInitDTO();
            if (classResultDTO != null) {
                initDTO.setCourseId(classResultDTO.getCourseId());
            }
            initDTO.setClassId(dto.getClassId());
            initDTO.setType(dto.getType());
            this.initCourseTotalScoreTypeConfig(initDTO);
            list = this.baseMapper.selectDtoList(wrapper, courseTotalScoreTypeConfigConvert::convert);
        }

        return list;
    }

    /**
     * 设置课程栏目等级分制
     */
    @Override
    public boolean setCourseTotalScoreTypeConfig(CourseTotalScoreConfigSetDTO dto) {
        // 先删除
        CourseTotalScoreConfigClassDTO classDTO = new CourseTotalScoreConfigClassDTO();
        classDTO.setClassId(dto.getClassId());
        classDTO.setType(dto.getType());
        this.baseMapper.deleteCourseTypeConfig(classDTO);
        // 后添加
        ClassResultDTO classResultDTO = classClient.findClass(dto.getClassId()).getData();
        List<CourseTotalScoreTypeConfigEntity> list = dto.getList().stream().map(v -> {
            CourseTotalScoreTypeConfigEntity entity = new CourseTotalScoreTypeConfigEntity();
            entity.setCourseId(classResultDTO.getCourseId());
            entity.setClassId(dto.getClassId());
            entity.setType(dto.getType());
            entity.setMax(v.getMax());
            entity.setMin(v.getMin());
            entity.setGrade(v.getGrade());
            entity.setSort(v.getSort());
            return entity;
        }).collect(Collectors.toList());
        return this.saveBatch(list);
    }

}
